package cn.huihuo.weibo.service;

import cn.huihuo.weibo.bean.ResponsePicBean;
import cn.huihuo.weibo.bean.productpric.LinkProduct;
import cn.huihuo.weibo.bean.productpric.ProductOutSitePrice;
import cn.huihuo.weibo.bean.productpric.ProductPrice;
import cn.huihuo.weibo.module.BuyChannel;
import com.google.gson.Gson;
import com.jfinal.kit.PropKit;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * 获取亚马逊商品的价格
 * Created by zhengjunbo on 2015/11/5.
 */
public class DownProductPrice {
    public static final DownProductPrice service = new DownProductPrice();
    private BlockingQueue<ProductOutSitePrice> queue = new ArrayBlockingQueue<>(1000);
    public DownProductPrice(){
        new Thread(() -> take()).start();
    }

    public boolean add(ProductOutSitePrice buyChannel){
        return queue.offer(buyChannel);
    }

    public void take(){

        while (true){
            ProductOutSitePrice buyChannel ;
            try {
                System.out.println("准备拿数据：");
                buyChannel = queue.take();
                System.out.println("获取的数据："+buyChannel);
            } catch (InterruptedException e) {
                e.printStackTrace();
                continue;
            }


            String picJson = null;
            try {
            picJson = getPicJson(buyChannel.getSite(),buyChannel.getUrl());
        } catch (Exception e) {
            e.printStackTrace();
            continue;
        }
        System.out.println("开始爬取外站价格"+picJson);

        Gson gson = new Gson();
        ProductPrice responsePicBean = gson.fromJson(picJson, ProductPrice.class);

        if (responsePicBean.getStatus().equals("1")) {
            BuyChannel buyChannel1 = new BuyChannel();
            LinkProduct linkProduct = responsePicBean.getLinkProduct();
            buyChannel1.set("channel_type",buyChannel.getSite());
            buyChannel1.set("channel_price",linkProduct.getPrice());
            buyChannel1.set("channel_freight",linkProduct.getShipPrice());
            buyChannel1.set("prod_id",buyChannel.getProdId());
            buyChannel1.set("channel_url",linkProduct.getUrl());
            buyChannel1.save();
        }
    }

    }

    public String getPicJson(String site,String url)  {

        String str ;
        try {
            HttpClient httpClient = new DefaultHttpClient();
            httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 120000);
            httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 120000);
            HttpGet get  = new HttpGet(PropKit.use("config.properties").get("priceRequest")+"?site="+site+"&&url="+url);
            HttpResponse execute = httpClient.execute(get);

            str = EntityUtils.toString(execute.getEntity(), "utf-8");
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

        return str;
    }
}
